package uk.co.q3c.deplan.client.domain.dag;

/*
 * Copyright The Codehaus Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
 * @version $Id: Vertex.java 8010 2009-01-07 12:59:50Z vsiveton $
 */

/**
 * @author David Sowerby 10 Jul 2010 Modified to encapsulate a taskKey object and use generics
 */
public class Vertex<T> implements Cloneable, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// ------------------------------------------------------------
	// Fields
	// ------------------------------------------------------------
	private T key = null;

	List<Vertex<T>> children = new ArrayList<Vertex<T>>();
	List<Vertex<T>> parents = new ArrayList<Vertex<T>>();

	// ------------------------------------------------------------
	// Constructors
	// ------------------------------------------------------------

	/**
     *
     */
	public Vertex(final T key) {
		this.key = key;
	}

	// ------------------------------------------------------------
	// Accessors
	// ------------------------------------------------------------

	/**
	 * @return
	 */
	public T getKey() {
		return key;
	}

	/**
	 * @param vertex
	 */
	public void addEdgeTo(final Vertex<T> vertex) {
		children.add(vertex);
	}

	/**
	 * @param vertex
	 * @return
	 */
	public boolean removeEdgeTo(final Vertex<T> vertex) {
		return children.remove(vertex);
	}

	/**
	 * @param vertex
	 */
	public void addEdgeFrom(final Vertex<T> vertex) {
		parents.add(vertex);
	}

	public boolean removeEdgeFrom(final Vertex<T> vertex) {

		return parents.remove(vertex);

	}

	public List<Vertex<T>> getChildren() {
		return children;
	}

	/**
	 * Get the list the most direct ancestors (parents).
	 * 
	 * @return list of parents
	 */
	public List<Vertex<T>> getParents() {
		return parents;
	}

	/**
	 * Indicates if given vertex has no child
	 * 
	 * @return <code>true</true> if this vertex has no child, <code>false</code> otherwise
	 */
	public boolean isLeaf() {
		return children.size() == 0;
	}

	/**
	 * Indicates if given vertex has no parent
	 * 
	 * @return <code>true</true> if this vertex has no parent, <code>false</code> otherwise
	 */
	public boolean isRoot() {
		return parents.size() == 0;
	}

	/**
	 * Indicates if there is at least one edee leading to or from given vertex
	 * 
	 * @return <code>true</true> if this vertex is connected with other vertex,<code>false</code> otherwise
	 */
	public boolean isConnected() {
		return isRoot() || isLeaf();
	}

	@Override
	public String toString() {
		return "Vertex{" + "key='" + key + "'" + "}";
	}

	public boolean hasParent(T key) {
		boolean found = false;
		for (Vertex<T> v : parents) {
			if (v.getKey()
					.equals(key)) {
				found = true;
				break;
			}
		}
		return found;
	}

	public boolean hasChild(T key) {
		boolean found = false;
		for (Vertex<T> v : children) {
			if (v.getKey()
					.equals(key)) {
				found = true;
				break;
			}
		}
		return found;
	}

	public boolean childOrderDown(T subTaskKey) {
		int currentPosition = indexOf(subTaskKey, children);
		if (currentPosition < 0) {
			return false;
		}
		if (currentPosition < children.size() - 1) {
			Vertex<T> v = children.remove(currentPosition);
			children.add(currentPosition + 1, v);
			return true;
		} else {
			return false;
		}

	}

	public boolean childOrderUp(T subTaskKey) {
		int currentPosition = indexOf(subTaskKey, children);
		if (currentPosition > 0) {
			Vertex<T> v = children.remove(currentPosition);
			children.add(currentPosition - 1, v);
			return true;
		} else {
			return false;
		}

	}

	private int indexOf(T key, List<Vertex<T>> list) {
		int index = -1;
		for (int i = 0; i < list.size(); i++) {
			Vertex<T> v = list.get(i);
			if (v.getKey()
					.equals(key)) {
				index = i;
				break;
			}
		}
		return index;
	}

	public int childCount() {
		return children.size();
	}

	public void clearParents() {
		parents.clear();
	}

	public int parentCount() {
		return parents.size();
	}

	public boolean hasChildren() {
		return children.size() > 0;
	}

}
